/**
 * 脚本主要功能是一键给微信运动中的好友点赞，核心算法来自一位网友，感谢原作者
 * 欢迎使用和提交bug反馈
 * 设备要求：
 * 1.需要root
 * 2.安卓5.0以上
 * 3.Auto.js软件版本3.0以上
 *
 * 使用方法：
 * 1.将脚本与Robot.js放于同一目录下
 * 2.将微信运动放于微信首页，最好置顶公众号
 * 3.直接启动脚本即可
 * 4.暂时不支持解锁手机
 * 5.脚本运行时，可以按Home键停止运行
 *
 * 软件测试结果：
 * 1.魔趣7.1系统正常，偶尔出现崩溃情况
 * @author ridersam <e1399579@gmail.com>
 */
const MAX_RETRY_TIMES = 10;
var face = ["[Kiss]","[Heart]","[Rose]","[Joyful]","[Grin]","[Drool]","[Lips]","[Determined]","[Laugh]","[Ruthless]"];
var options = {
    sendName: "慧宝", // 
    sendContent:"媳妇，早上好", // 发送内容
    sendNickName:"媳妇",
    sendDay:0,
    sendJieQi:"",
    sendFace:"",
    timeout: 6000, // 查找控件超时时间
    max_retry_times: MAX_RETRY_TIMES, // 最大重试次数
};

auto();
// require("unlock.js");

setTimeout(function(){
    start(options);
},3*1000);


function start(options) {

    var JieQi = require("./jieqi.js");

    var currentDay = new Date();

    var preDate = new Date(currentDay.getTime() - 24*60*60*1000); //前一天

    var jieRi = new JieQi();

    var currentJieri = jieRi.getjq(currentDay.getFullYear(),currentDay.getMonth()+1,currentDay.getDate());
    var preJieri = jieRi.getjq(preDate.getFullYear(),preDate.getMonth()+1,preDate.getDate());
    console.log(currentJieri);
    console.log(preJieri);
    if(currentJieri==preJieri){
       
    }else{
        console.log("不相等");
        var NAME = this.options.sendNickName;
        var EMOTION= "爱情";
        var MEDIA = "微信"
        var ME = "老公"
        this.options.sendJieQi = jieRi.getSendMsg(currentJieri,0);
        this.options.sendJieQi = this.options.sendJieQi.replace(/NAME/g,NAME);
        this.options.sendJieQi = this.options.sendJieQi.replace(/EMOTION/g,EMOTION);
        this.options.sendJieQi = this.options.sendJieQi.replace(/MEDIA/g,MEDIA);
        this.options.sendJieQi = this.options.sendJieQi.replace(/ME/g,ME);
    }
    this.options.sendDay = jieRi.datedifference("2020-08-15",currentDay);
    console.log(this.options.sendDay+"");
    var num = parseInt(this.options.sendDay);
    console.log(num+"");
    var a = parseInt(num % 10); // 个位数
    var b = parseInt((num % 100) / 10); // 十位数
    var c = parseInt((num % 1000) / 100); // 百位数
    var d = parseInt((num % 10000) / 1000); // 千位数
    var e = parseInt((num % 10000) / 10000); // 万位数
    console.log(num+"a:"+e+d+c+b+a);
    console.log(this.face[e]+this.face[d]+this.face[c]+this.face[b]+this.face[a]);
    
    if(b>=8){
        for (var i=0;i<this.face.length;i++) {
           this.options.sendFace = this.options.sendFace+this.face[i];
        }
        this.options.sendFace = this.options.sendFace+"\n"+this.face[e]+this.face[d]+this.face[c]+this.face[b]+this.face[a];
    
    } else {
        this.options.sendFace = this.face[e]+this.face[e]+this.face[e]+this.face[e];
    }
    var Robot = require("./Robot.js");

    if (!Robot) {
        throw new Error("缺少Robot.js文件");
    }

    var robot = new Robot(MAX_RETRY_TIMES);
    var weChatSendMsg = new WeChatSendMsg(robot,options);

    // 子线程监听Home键
    threads.start(function () {
        events.observeKey();
        events.onKeyDown("home", function (event) {
            console.log("停止脚本");
            engines.stopAll();
            exit();
        });
    });

    weChatSendMsg.launch();
    engines.stopAll();
    exit();
}

function WeChatSendMsg(robot,options){
    this.robot = robot;
    options = options || {};
    var settings = {
        timeout: 3000,
        max_retry_times: 10,
        min_step_count: 0,
    };
    this.options = Object.assign(settings, options);
    this.package = "com.tencent.mm";
    this.activity = "com.tencent.mm.plugin.exdevice.ui.ExdeviceRankInfoUI";
    this.btns1 = [options.sendName];
    this.step = 0;
    this.launch = function () {
        console.log("即将进入微信，按Home键停止");

        launch("com.tencent.mm");

        var btn;
        var isHome = false;
        var step = 6;
        do{

            var btn1 = text("微信").visibleToUser(true).findOne(500);
            var btn2 = text("通讯录").visibleToUser(true).findOne(500);
            var btn3 = text("发现").visibleToUser(true).findOne(500);
    
            if(btn1 !=null && btn3!=null && btn2!=null){
                isHome = true;
                console.log("进入到微信页面");
                id("dub").className("android.widget.TextView").text("微信").findOne().parent().click();
                // this.robot.clickCenter(btn1);
                sleep(200);
            }else{
                back();
            }
            step--;
            console.log(""+step);
            if(step ==0){
                return false;
            }
            
        }while(!isHome);
        console.log("开始查找");
        btn = text(this.options.sendName).visibleToUser(true).findOne(10*1000);
        if (btn) {
            this.robot.clickCenter(btn);
            sleep(500);
        } else {
            btn = desc("雷勋").visibleToUser(true).findOne(5000);
            if(btn){
                console.log("desc进入" + this.options.sendName + "页面失败");
                btn.click();
            }else{
                console.log("进入" + this.options.sendName + "页面失败");
                return false;
            }
           
        }
        
       
        if(this.options.sendJieQi.length==0){
            className("EditText").findOne().setText(this.options.sendContent+"\n"+this.options.sendFace);
        }else{
            className("EditText").findOne().setText(this.options.sendContent+"\n"+this.options.sendFace+"\n"+this.options.sendJieQi);
        }
        
        text("发送").click();
        console.log("点击输入框成功");
        sleep(500);
        this.back();

        // this.robot.kill(this.package);
        // exit();
        return false;
    };

    this.doLaunch = function (btns) {
        launch("com.tencent.mm");

        do {
            var btn;
            btn = text(btns[0]).visibleToUser(true).findOne(this.options.timeout);
            if (btn) {
                this.robot.clickCenter(btn);
                btns.shift();
                sleep(500);
            } else {
                console.log("进入" + btns[0] + "页面失败");
                return false;
            }
        } while (btns.length > 0);

        return this.isRank();
    };

    this.work = function () {
        console.log("开始点赞");

        do {
            className("android.widget.ImageView").depth(17).visibleToUser(true).filter(function (o) {
                return 1 === o.indexInParent();
            }).find().forEach(function (o) {
                var btn = o.parent().parent();
                btn.click();

                if (btn.parent()) {
                    var step_count = btn.parent().findOne(textMatches(/\d+/)).text(); // 当前好友的步数
                    if (step_count <= this.options.min_step_count) {
                        return;
                    }
                }

                // 若是误点进入了详情，后退
                if (!this.isRank()) {
                    this.back();
                    sleep(500);
                }
            }.bind(this));

            sleep(2000);
        } while (this.nextPage());

        // 返回主界面
        for (var i = 0; i < this.step; i++) {
            this.back();
            sleep(500);
        }

        toast("执行完毕");
    };

    this.isRank = function () {
        sleep(200);
        return currentActivity() === this.activity;
    }

    this.nextPage = function () {
        return packageName(this.package).className("android.widget.ListView").scrollDown();
    };

    this.back = function () {
        var y = device.height / 2;
        this.robot.swipe(0, y, 100, y, 10);
    }
}

